package com.hedd.shop.interceptor;


import com.hedd.shop.constants.Constants;
import com.hedd.shop.exceptions.ShopException;
import com.hedd.shop.pojo.vo.RespResult;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request
            , HttpServletResponse response, Object handler) throws Exception {
        //获取session对象
        System.out.println("doingfilter");
        HttpSession session = request.getSession();
        //判断session中用户信息
        if (null == session || null == session.getAttribute(Constants.SESSION_KEY_LOGIN_USER)) {
            //用户未登录
            throw new ShopException(RespResult.Code.UN_LOGIN.getValue(), "用户未登录");
        }
        return true;
    }
}
